package com.currency.mall.ui.main.order;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.currency.mall.Constant;
import com.currency.mall.R;
import com.currency.mall.entity.OrderInfo;
import com.currency.mall.event.OrderEvent;
import com.currency.mall.netWork.NetWork;
import com.currency.mall.util.MD5Util;
import com.mifengs.commonbase.base.BaseActivity;
import com.mifengs.commonbase.util.StatusBarCompat;
import com.mifengs.commonbase.util.StatusBarUtils;
import com.mifengs.commonbase.util.ToastUitl;
import com.mifengs.commonbusines.entity.ResponseBean;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/12/27.
 */

public class EntrustSettlementActivity extends BaseActivity {
    @BindView(R.id.tv_nominal_principal)
    TextView tvNominalPrincipal;
    @BindView(R.id.tv_royalty_rate)
    TextView tvRoyaltyRate;
    @BindView(R.id.tv_current_price)
    TextView tvCurrentPrice;
    @BindView(R.id.tv_floating_profit)
    TextView tvFloatingProfit;
    @BindView(R.id.tv_option_cycle)
    TextView tvOptionCycle;
    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.tv_expire_time)
    TextView tvExpireTime;
    @BindView(R.id.btn_subscribe)
    Button btnSubscribe;
    private OrderInfo orderInfo;
    private Dialog pwdDilaog;
    private EditText etPwd;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_entrust_settlement);
        ButterKnife.bind(this);
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(getContext(), R.color.title_color));
        StatusBarUtils.StatusBarLightMode(this);
        orderInfo = (OrderInfo) getIntent().getSerializableExtra("orderInfo");
        init();
    }

    private void init() {
        if (orderInfo != null) {
            tvNominalPrincipal.setText(orderInfo.getNameSum() + "元");
            tvRoyaltyRate.setText(orderInfo.getRightSum() + "元");
            tvCurrentPrice.setText(orderInfo.getPriceNow() + "元");
            tvFloatingProfit.setText(orderInfo.getFloatProfit() + "元");
            if ("DAY".equals(orderInfo.getRoyaltyType())) {
                tvOptionCycle.setText(orderInfo.getRoyaltyVal() + "天");
            } else if ("WEEK".equals(orderInfo.getRoyaltyType())) {
                tvOptionCycle.setText(orderInfo.getRoyaltyVal() + "周");
            } else if ("MONTH".equals(orderInfo.getRoyaltyType())) {
                tvOptionCycle.setText(orderInfo.getRoyaltyVal() + "月");
            }
            tvStartTime.setText(orderInfo.getBeginTime());
            tvExpireTime.setText(orderInfo.getEndTime());
        }

    }

    private void pwdDilaogShow() {
        pwdDilaog = new Dialog(getContext(), R.style.dialog);
        pwdDilaog.setContentView(R.layout.dialog_set_pwd);
        pwdDilaog.show();
        pwdDilaog.setCancelable(false);
        etPwd = (EditText) pwdDilaog.findViewById(R.id.et_input_pwd);
        Button okBtn = pwdDilaog.findViewById(R.id.bt_ok);
        okBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pwdDilaog.dismiss();
                sumbitSettlementOrder();
            }
        });
        Button cancleBtn = pwdDilaog.findViewById(R.id.bt_cancle);
        cancleBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pwdDilaog.dismiss();
            }
        });
    }


    public void sumbitSettlementOrder() {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("orderId", orderInfo.getId());
        hashMap.put("password", MD5Util.toMD5(etPwd.getText().toString()).toLowerCase());
        NetWork.getOderApi().settlement(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUitl.show(e.getMessage(),200);
                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        if (responseBean.getCode().equals(Constant.SUCCESS_CODE)) {
                            ToastUitl.show("委托结算成功", 200);
                            EventBus.getDefault().post(new OrderEvent(OrderEvent.ORDER_TASK_STATU,0));
                            finish();
                        } else {
                            ToastUitl.show(responseBean.getMsg(), 200);
                        }
                    }
                });
    }


    @OnClick({R.id.ll_back, R.id.btn_subscribe})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.btn_subscribe:
                pwdDilaogShow();
                break;
        }
    }


}
